package com.blockchain.securewallet.view;

import android.content.Context;
import android.graphics.ColorMatrixColorFilter;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;

import com.blockchain.securewallet.utils.Logger;

/**
 * Created by Chain-Cloud on 2018/5/16.
 */

public class SelectorButton extends Button {

    public SelectorButton(Context context) {
        super(context);
        this.setOnTouchListener(VIEW_TOUCH_DARK);
    }

    public SelectorButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.setOnTouchListener(VIEW_TOUCH_DARK);
    }

    public OnTouchListener VIEW_TOUCH_DARK = new OnTouchListener() {
        //变暗(三个-50，值越大则效果越深)
        public final float[] BT_SELECTED_DARK = new float[]{1, 0, 0, 0, -50, 0, 1,
                0, 0, -50, 0, 0, 1, 0, -50, 0, 0, 0, 1, 0};

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            int action = event.getAction();
            //Logger.ds(">> 此时的action: " + action);

            if (action == MotionEvent.ACTION_DOWN) {
                Button iv = (Button) v;
                if (iv == null) {
                    Logger.ds("ACTION_DOWN - View为空!!");
                    return true;
                }
                iv.getBackground().setColorFilter(new ColorMatrixColorFilter(BT_SELECTED_DARK));
                //Logger.ds("ACTION_DOWN: " + 0);

            } else if (action == MotionEvent.ACTION_UP) {
                Button iv = (Button) v;
                if (iv == null) {
                    Logger.ds("ACTION_UP - View为空!!");
                    return true;
                }

                if (iv.getBackground() == null) {
                    Logger.ds("ACTION_UP - iv.getBackground为空!!");
                    return true;
                }

                try {
                    iv.getBackground().clearColorFilter();
                } catch (Exception e) {
                    e.printStackTrace();
                    Logger.ds("error: " + e.getMessage());
                    return true;
                }

                //获得当前触控点所在的坐标，这也是以o点为准的
                float relativeX = event.getX();
                float relativeY = event.getY();
                //float currentX = event.getRawX();
                //float currentY = event.getRawY();
                //Logger.ds(">location:\t原生的X: " + currentX + "\t原生的Y: " + currentY);
                //Logger.ds(">location:\t相对的X: " + relativeX + "\t相对的Y: " + relativeY);

                //获得当前触摸控件的宽高
                int width = v.getWidth();
                int height = v.getHeight();

                //以o点为准，o点为View的左上角的坐标，需要注意的是，这里的View的x,y坐标，是以view的最近父容器为准的
                //Logger.ds(">view:\twidth: " + width + "\theight: " + height + "\n\t\t\t\t\t\t\t>point o:\tv.X: " + v.getX() + "\tv.Y:" + v.getY());

                //对这2个方向上是否在View外进行判断
                // 相对
                boolean targetX = !(relativeX > 0 && relativeX < width);
                boolean targetY = !(relativeY > 0 && relativeY < height);
                //当释放手势时，所在坐标大于控件所在的坐标时，不触发View的点击事件
                //这里，只要有1个方向的触控坐标点不在View的范围内，则表示在View外面了
                boolean isOutside = targetX || targetY;
                //Logger.ds(">> targetX is outside: " + targetX + "\t\ttargetY is outside: " + targetY);

                if (isOutside) {
                    //Nothing can be done 
                    //Logger.ds("do not click: " + isOutside);
                } else {
                    //Logger.ds("View.onClick(): " + isOutside);
                    mPerformClick();
                }

                //Logger.ds("ACTION_UP: " + 1);

            } else if (action == MotionEvent.ACTION_CANCEL) {
                Button iv = (Button) v;
                iv.getBackground().clearColorFilter();
                //Logger.ds("ACTION_CANCEL: " + 3);

            } else if (action == MotionEvent.ACTION_OUTSIDE) {
                Button iv = (Button) v;
                iv.getBackground().clearColorFilter();
                //Logger.ds("ACTION_OUTSIDE: " + 4);
            }

            return true;  //如为false，执行ACTION_DOWN后不再往下执行
        }
    };

    private void mPerformClick() {
        SelectorButton.this.performClick();
    }


}
